const fileService = require("../service/file.service");
const { AVATAR_PATH } = require('../constants/file-path');
const userService = require("../service/user.service");
const { APP_HOST, APP_PORT } = require('../app/config');


class FileController {
    async saveAvatarInfo(ctx, next) {
        //获取图像数据
        const { mimetype, filename, size } = ctx.req.file;
        const { id } = ctx.user;

        //将图像信息数据保存到数据库中
        const result = await fileService.createAvatar(filename, mimetype, size, id);
        //将图片地址保存到users表中
        const avatarUrl = `${APP_HOST}:${APP_PORT}/users/${id}/avatar`;
        await userService.updateAvatarUrlById(avatarUrl, id);

        //返回响应结果
        ctx.body = '上传头像成功';
    }

    async savePictureInfo(ctx, next) {
        const files = ctx.req.files;
        const { id } = ctx.user;
        const { momentId } = ctx.query;

        for (let file of files) {
            const { filename, mimetype, size } = file;
            await fileService.createFile(filename, mimetype, size, id, momentId);
        }

        ctx.body = "上传成功";
    } 
}

module.exports = new FileController();